package com.kwum.toutiao;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kwum.TimeUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Description: 
 * @Company: yimo
 * @author: Kwum
 * @date 2018年2月12日 下午3:08:35
 */

public class TouTiaoCommentsDemo {

	public static void main(String[] args) throws Exception {
		List<TouTiaoComment> list = getTouTiaoComments("6520174326887481870");
		System.out.println(list.size());
	}
	
	public static List<TouTiaoComment> getTouTiaoComments(String pgcId) throws Exception{
		
		List<TouTiaoComment> list = new ArrayList<TouTiaoComment>();
		
		//获取评论列表
        //第一页
		Document document = Jsoup.connect("http://is.snssdk.com/article/v2/tab_comments/?"
				+ "group_id=" + pgcId + "&"
				+ "item_id=" + pgcId + "&"
				+ "aggr_type=1&count=50&"
				+ "offset=0"
				+ "&tab_index=0&fold=1&iid=26205755493&device_id=48316220153&ac=wifi&channel=tengxun&"
				+ "aid=13&app_name=news_article&version_code=625&version_name=6.2.5&device_platform=android&"
				+ "ab_version=273718%2C277893%2C252766%2C249828%2C246859%2C278039%2C249685%2C249686%2C249675%2C264843%2C206076%2C249673%2C272432"
				+ "%2C229304%2C249672%2C280057%2C277467%2C280418%2C271717%2C240867%2C280773%2C185730%2C280475%2C272683%2C251796%2C280931%2C251714%"
				+ "2C277523%2C31642%2C251454%2C279126%2C270336%2C280960%2C280220%2C280122%2C280134%2C258356%2C247848%2C264452%2C276964%2C249045%2C"
				+ "210683%2C281000%2C244746%2C280034%2C264615%2C260657%2C281110%2C280982%2C277519%2C232362%2C265706%2C279475%2C277647%2C239098%2C2"
				+ "72014%2C170988%2C269425%2C273496%2C279386%2C280269%2C276204%2C281101%2C257284%2C261293%2C265278%2C280147%2C278670&"
				+ "ab_client=a1%2Cc4%2Ce1%2Cf2%2Cg2%2Cf7&ab_feature=94563%2C102749&abflag=3&ssmix=a&device_type=GT-I9260&device_brand=samsung&"
				+ "language=zh&os_api=17&os_version=4.2.2&uuid=864895028532693&openudid=816b61a09ab9f477&manifest_version_code=625&"
				+ "resolution=1080*1920&dpi=320&update_version_code=6254&"
				+ "_rticket=" + System.currentTimeMillis())
								.ignoreContentType(true)
								.get();
		JSONArray commentJson = JSONObject.fromObject(document.text()).getJSONArray("data");
		for(int i = 0; i < commentJson.size(); i++) {
			JSONObject object = commentJson.getJSONObject(i).getJSONObject("comment");
			System.out.println(new TouTiaoComment(object.getString("user_name"), object.getString("text"), object.optInt("digg_count", 0), 
					object.optInt("reply_count", 0), TimeUtils.timestamp2String(object.getLong("create_time"))));
			list.add(new TouTiaoComment(object.getString("user_name"), object.getString("text"), object.optInt("digg_count", 0), 
					object.optInt("reply_count", 0), TimeUtils.timestamp2String(object.getLong("create_time"))));
		}
		while(commentJson.size() != 0) {
			int setoff = (list.size() / 50 + 1) * 50;
	    	document = Jsoup.connect("http://is.snssdk.com/article/v2/tab_comments/?"
					+ "group_id=" + pgcId + "&"
					+ "item_id=" + pgcId + "&"
					+ "aggr_type=1&count=50&"
					+ "offset=" + setoff
					+ "&tab_index=0&fold=1&iid=26205755493&device_id=48316220153&ac=wifi&channel=tengxun&"
					+ "aid=13&app_name=news_article&version_code=625&version_name=6.2.5&device_platform=android&"
					+ "ab_version=273718%2C277893%2C252766%2C249828%2C246859%2C278039%2C249685%2C249686%2C249675%2C264843%2C206076%2C249673%2C272432"
					+ "%2C229304%2C249672%2C280057%2C277467%2C280418%2C271717%2C240867%2C280773%2C185730%2C280475%2C272683%2C251796%2C280931%2C251714%"
					+ "2C277523%2C31642%2C251454%2C279126%2C270336%2C280960%2C280220%2C280122%2C280134%2C258356%2C247848%2C264452%2C276964%2C249045%2C"
					+ "210683%2C281000%2C244746%2C280034%2C264615%2C260657%2C281110%2C280982%2C277519%2C232362%2C265706%2C279475%2C277647%2C239098%2C2"
					+ "72014%2C170988%2C269425%2C273496%2C279386%2C280269%2C276204%2C281101%2C257284%2C261293%2C265278%2C280147%2C278670&"
					+ "ab_client=a1%2Cc4%2Ce1%2Cf2%2Cg2%2Cf7&ab_feature=94563%2C102749&abflag=3&ssmix=a&device_type=GT-I9260&device_brand=samsung&"
					+ "language=zh&os_api=17&os_version=4.2.2&uuid=864895028532693&openudid=816b61a09ab9f477&manifest_version_code=625&"
					+ "resolution=1080*1920&dpi=320&update_version_code=6254&"
					+ "_rticket=" + System.currentTimeMillis())
									.ignoreContentType(true)
									.get();
	    	commentJson = JSONObject.fromObject(document.text()).getJSONArray("data");
	    	for(int i = 0; i < commentJson.size(); i++) {
				JSONObject object = commentJson.getJSONObject(i).getJSONObject("comment");
				System.out.println(new TouTiaoComment(object.getString("user_name"), object.getString("text"), object.optInt("digg_count", 0), 
						object.optInt("reply_count", 0), TimeUtils.timestamp2String(object.getLong("create_time"))));
				list.add(new TouTiaoComment(object.getString("user_name"), object.getString("text"), object.optInt("digg_count", 0), 
						object.optInt("reply_count", 0), TimeUtils.timestamp2String(object.getLong("create_time"))));
			}
		}
		return list;
	}
}
